package magicloan.auctionpocket.ui.activity;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.utils.module.StringUtils;

import magicloan.auctionpocket.R;
import magicloan.auctionpocket.view.textview.CountDownTimer;

import static magicloan.auctionpocket.R.id.tv_msg;

/**
 * Created by hechao on 2018/1/25.
 */
public class LoginActivity extends magicloan.auctionpocket.ui.base.BaseActivity{

    private EditText mEt_mobile , mEt_msg , mEt_img ;
    private String mMobie , mMsg , mImg ;
    private TextView mTv_login , mTv_msg;
    private ImageView mImg_confirm ;

    @Override
    protected int setContentView() {
        return R.layout.ac_register ;
    }

    @Override
    protected void beforeInitView() {

    }

    @Override
    protected void afterInitView() {

        tv_public_title.setText("登录/注册");

        mEt_mobile = (EditText) mContentView.findViewById(R.id.et_mobile);
        mEt_msg = (EditText) mContentView.findViewById(R.id.et_msg);
        mEt_img = (EditText) mContentView.findViewById(R.id.et_img);

        mEt_mobile.addTextChangedListener(new MyEditTextChangeListener());
        mEt_msg.addTextChangedListener(new MyEditTextChangeListener());
        mEt_img.addTextChangedListener(new MyEditTextChangeListener());

        mTv_login = (TextView) mContentView.findViewById(R.id.tv_login);
        mImg_confirm = (ImageView) mContentView.findViewById(R.id.img_confirm);
        mTv_msg = (TextView) mContentView.findViewById(tv_msg);

        mImg_confirm.setOnClickListener(this);
        mTv_login.setOnClickListener(this);
        mTv_msg.setOnClickListener(this);

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch ( v.getId() ){
            case tv_msg:
                startCountTime();
                break;
            case R.id.img_confirm:
                break;
            case R.id.tv_login:
                break;
        }
    }

    private void startCountTime() {
        MyCount myCount = new MyCount(60000, 1000);
        myCount.start();
    }

    private class MyCount extends CountDownTimer {
        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            mTv_msg.setText("获取验证码");
            mTv_msg.setClickable(true);
            mTv_msg.setTextColor(mActivity.getResources().getColor(R.color.color_f4ab51));
        }

        @Override
        public void onTick(long millisUntilFinished) {
            mTv_msg.setTextColor(mActivity.getResources().getColor(R.color.color_texthintcolor));
            mTv_msg.setText(millisUntilFinished / 1000 + "秒后重发");
            mTv_msg.setClickable(false);
        }
    }

    public class MyEditTextChangeListener implements TextWatcher {

        /**
         * 编辑框的内容发生改变之前的回调方法
         */
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        /**
         * 编辑框的内容正在发生改变时的回调方法 >>用户正在输入
         * 我们可以在这里实时地 通过搜索匹配用户的输入
         */
        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        /**
         * 编辑框的内容改变以后,用户没有继续输入时 的回调方法
         */
        @Override
        public void afterTextChanged(Editable editable) {
            mMobie = mEt_mobile.getText().toString().trim() ;
            mImg = mEt_img.getText().toString().trim() ;
            mMsg = mEt_msg.getText().toString().trim() ;
            checkEditInput();
        }

    }

    private void checkEditInput(){
        if ( !StringUtils.isEmpty(mMobie) && !StringUtils.isEmpty(mImg) && !StringUtils.isEmpty(mMsg) ){
            mTv_login.setBackgroundResource(R.drawable.shape_redconner_redfilll_30conner);
        }else{
            mTv_login.setBackgroundResource(R.drawable.shape_grayconner_grayfilll_30conner);
        }
    }

}
